/*
 * StorageManager.h
 */

#ifndef STORAGEMANAGER_H_
#define STORAGEMANAGER_H_

#include "Student.h"
#include "Page.h"

class StorageManager {

public:
	static const int NUMBER_OF_PAGES = 2001000;
	//1024 bytes per page, 8 bits per byte, 8192 bits per page = map slots per spacemap page
	//256 bytes per page == 2001000 pages, 978 spacemap pages

	StorageManager();
	~StorageManager();

	Page* AllocatePage();
	bool DeallocatePage(Page &pg);
	void PrintSpaceMap(int numRecordsToPrint, ostream &out); //-1 == all

	//Hard-coded convenience functions for project
	void PrintStudent(Page &p);
	Student *FetchStudent(Page &p);
	Record *FetchRecord(int recordPgNum); //input number is 0-indexed based on first record location
	Student *FetchStudent(int recordPgNum); //input number is 0-indexed based on first record location

private:
	typedef struct record_structure
	{
		int num_pages;
		int page_size; //size in bytes
		int num_bits_per_int;
		int num_spacemap_pages;
	} record_structure_t;
	typedef record_structure_t* record_structure_ptr;

	void InitializeSpaceMap();
	void ClearPage(Page *pg);
	int GetFreePageNumber();
	void MarkPage(int pageNum, bool used);
	bool IsUsed(int pageNum);

	Page *_bookRoot;
	record_structure_ptr _recordStructure;
	Page *_fileMapPage;
	Page *_firstSpaceMapPage;
	Page *_firstRecordPage;
	int _nextAvailablePage; //numbered pages starting with record structure on page 0
};

#endif /* STORAGEMANAGER_H_ */
